import axios from 'axios'



axios.defaults.baseURL = window.$app.baseURL
axios.defaults.headers.common['Clientid'] = window.$app.clientId

axios.interceptors.request.use(
  function (config) {
    // $ui.log.info(`发送接口请求【${config.url.replace($app.baseURL, '')}】`, config)

    if (window.$app.token) {
      config.headers.Authorization = `Bearer ${window.$app.token}`
    }

    if (window.$app.user) {
      config.headers['X-User-Account'] = window.$app.user.userId
    }

    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

axios.interceptors.response.use(
  function (response) {
    if (response.data.code === 401) {
      window.$app.logoff()
    }

    // $ui.log.info(`接口响应数据【${response.config.url.replace($app.baseURL, '')}】`, response.data)
    if (response.request?.responseURL.includes('zn-e-library')) {
      return response.data
    }
    return response
  },

  function (error) {
    // $ui.log.info(`接口响应错误【${error.config.url.replace($app.baseURL, '')}】`, error)
    return Promise.reject(error)
  }
)

export default axios

export const CODE_MAP = {
  SUCCESS: '200',
  ERROR: '500',
  NO_AUTH: '403',
  ERR_AUTH: '1001',
  NO_LOGIN: [4210, 4211]
}